package com.mercury.pattern._16_observer;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class WeatherData implements Subject {
    List<Observer> list;

    private float temperature;
    private float pressure;
    private float humidity;

    public WeatherData() {
        list = new ArrayList<>();
    }

    @Override
    public void registerObserver(Observer o) {
        list.add(o);
    }

    @Override
    public void removeObserver(Observer o) {
        list.remove(o);
    }

    @Override
    public void notifyObservers() {
        list.stream().forEach(observer -> observer.update(this.temperature,this.pressure,this.humidity));
    }

    //当数据有更新时，就调用 setData
    public void setData(float temperature, float pressure, float humidity) {
        this.temperature = temperature;
        this.pressure = pressure;
        this.humidity = humidity;
//调用 dataChange， 将最新的信息 推送给 接入方 currentConditions
        dataChange();
    }

    public void dataChange() {
        notifyObservers();
    }

    public float getTemperature() {
        return temperature;
    }

    public void setTemperature(float temperature) {
        this.temperature = temperature;
    }

    public float getPressure() {
        return pressure;
    }

    public void setPressure(float pressure) {
        this.pressure = pressure;
    }

    public float getHumidity() {
        return humidity;
    }

    public void setHumidity(float humidity) {
        this.humidity = humidity;
    }
}